//
//  CustomReportView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomReportView.h"


@implementation CustomReportView
@synthesize startDate,endDate,babyActivities,babyActivitiesSummary;

- (void)viewDidLoad {
	self.title = NSLocalizedString(@"Detail Report", @"");
    [super viewDidLoad];
	self.babyActivities        = [[NSMutableArray alloc]init];
	self.babyActivitiesSummary = [[BabyActivityReportEntry alloc]init];

	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
	Dao *dao = [Dao getInstance];
	
	double startTimeValue = (double)[self.startDate timeIntervalSince1970];
	double endTimeValue   = (double)[self.endDate timeIntervalSince1970];	
	NSString *whereClause = [NSString stringWithFormat:@"WHERE timestamp between %f and %f ORDER BY TIMESTAMP DESC",startTimeValue,endTimeValue];
	NSLog(@"%@",whereClause);
	[dao QueryBabyActivityWith:whereClause records:self.babyActivities];
	[dao GetReportSummaryWithStartEndDate:self.startDate endDate:self.endDate report:self.babyActivitiesSummary];
	
	[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.startDate = nil;
	self.endDate = nil;
	self.babyActivities = nil;
	self.babyActivitiesSummary =nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

	//ToJSON Test
	if([self.babyActivities count] > 0)
	{
		//NSArray *array = [[NSArray alloc]initWithArray:self.babyActivities];
		NSArray *array = [NSArray arrayWithArray:self.babyActivities];
		NSString *json = [array JSONRepresentation];
		NSLog(@"%@",json);
	}
					  
	
	if([self.babyActivities count] == 0)
		return 1;
	
	if( section == 0  )
		return 5;
	else
		return [babyActivities count];
}


- (NSString *)tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)section
{
	if( section == 0  )
		return NSLocalizedString(@"Summary", @"");
	else
		return NSLocalizedString(@"Details", @"");
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	if([self.babyActivities count] == 0)
	{
		cell.textLabel.text = NSLocalizedString(@"No record according rule", @"");
		cell.backgroundColor = [UIColor colorWithRed:cellBackgroundReportRed green:cellBackgroundReportGreen 
												blue:cellBackgroundReportBlue alpha:cellBackgroundReportAlpha];
				
		return cell;
	}
	
	
	switch (indexPath.section) {
		case 0:
		{
			switch (indexPath.row) {
				case 0:
					cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Mum       %d times       %d min ",@""),self.babyActivitiesSummary.countOfEatMum,self.babyActivitiesSummary.amountOfEatMum];
					cell.imageView.image = [UIImage imageNamed:@"mum"];
					break;
				case 1:
					cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Milk       %d times       %d ml",@""),self.babyActivitiesSummary.countOfEatMilk,self.babyActivitiesSummary.amountOfEatMilk];
					cell.imageView.image = [UIImage imageNamed:@"milk_bottle"];
					break;
				case 2:
					cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Water       %d times       %d ml",@""),self.babyActivitiesSummary.countOfDrinkWater,self.babyActivitiesSummary.amountOfDrinkWater];
					cell.imageView.image = [UIImage imageNamed:@"DrinkWater"];
					break;
				case 3:
					cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Pee       %d times",@""),self.babyActivitiesSummary.countOfPee];
					cell.imageView.image = [UIImage imageNamed:@"pee"];
					break;
				case 4:
					cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Shit       %d times",@""),self.babyActivitiesSummary.countOfShit];
					cell.imageView.image = [UIImage imageNamed:@"shit"];
					break;
				default:
					break;
			}
			break;
		}
		case 1:
		{
			BabyActivityEntry *entry = [self.babyActivities objectAtIndex:indexPath.row];	
			cell.textLabel.text = [NSString stringWithFormat:@"%@     %@     %@", [entry getTimeStringWithOutYear],[entry getCategoryName],[entry getAmountDetails]];
			cell.detailTextLabel.text = entry.comments;    //Max 20 chinese character.
			cell.detailTextLabel.textColor = [UIColor blueColor];
		
			switch (entry.category) {
				case EAT_MUM_CATEGORY:
					cell.imageView.image = [UIImage imageNamed:@"mum"];
					break;
				case EAT_MILK_CATEGORY:
					cell.imageView.image = [UIImage imageNamed:@"milk_bottle"];
					break;
				case DRINK_WATER_CATEGORY:
					cell.imageView.image = [UIImage imageNamed:@"DrinkWater"];
					break;
				case PEE_CATEGORY:
					cell.imageView.image = [UIImage imageNamed:@"pee"];
					break;
				case SHIT_CATEGORY:
					cell.imageView.image = [UIImage imageNamed:@"shit"];
					break;
				default:
					cell.imageView.image = nil;
					break;
			}
		}
			break;
		default:
			break;
	}
	
	cell.backgroundColor = [UIColor colorWithRed:cellBackgroundReportRed green:cellBackgroundReportGreen 
											blue:cellBackgroundReportBlue alpha:cellBackgroundReportAlpha];

    return cell;
}

- (void)dealloc {
	[self.babyActivities release];
	[self.babyActivitiesSummary release];
    [super dealloc];
}


@end

